<template>
  <div class="content">
    <div class="minContent" ref="echarts"></div>
    <div class="point" :style="comStyle">
      <span class="left" @click="preview" ref="left"> < </span>
      <span class="right" @click="next" ref="right"> > </span>
    </div>
    <span class="title" :style="comStyle">{{ title }}</span>
  </div>
</template>
<script>
export default {
  data() {
    return {
      chartInstance: null,
      data: [],
      currentType: 0,
      title: "",
      titleSize: 0,
    };
  },
  computed: {
    comStyle() {
      return {
        fontSize: this.titleSize + "px",
      };
    },
  },
  mounted() {
    this.init();
    this.getData();
    window.addEventListener("resize", this.widthChange);
    this.widthChange();
  },
  destroyed() {
    window.removeEventListener("resize", this.widthChange);
  },
  methods: {
    init() {
      this.chartInstance = this.$echarts.init(this.$refs.echarts, "chalk");
      const initOption = {
        title: {
          text: "▍ 热销商品销售金额占比统计",
          left: 20,
          top: 20,
        },
        series: [
          {
            type: "pie",
            top: 80,
            label: {
              show: false,
            },
            emphasis: {
              label: {
                show: true,
              },
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);
    },
    async getData() {
      const { data: res } = await this.$http.get("hotproduct");
      this.data = res;
      console.log(res);
      this.updateEchart();
    },
    updateEchart() {
      this.title = this.data[this.currentType].name;
      const legendData = this.data[this.currentType].children.map((v) => {
        return { icon: "circle", name: v.name };
      });
      var dataOption = {
        series: [
          {
            data: this.data[this.currentType].children,
          },
        ],
        legend: {
          data: legendData,
          left: "center",
          top: "15%",
        },
        tooltip: {
          formatter: (arg) => {
            let totalSum = 0;
            arg.data.children.forEach((v) => {
              totalSum += v.value;
            });
            let str = "";
            arg.data.children.map(
              (v) =>
                (str +=
                  v.name +
                  "&nbsp" +
                  "&nbsp" +
                  parseInt((v.value / totalSum) * 100) +
                  "%" +
                  "<br/>")
            );
            return str;
          },
        },
      };
      this.chartInstance.setOption(dataOption);
    },
    widthChange() {
      //3. 自适应配置
      this.titleSize = (this.$refs.echarts.offsetWidth / 100) * 3.6;
      const sizeOption = {
        title: {
          textStyle: {
            fontSize: this.titleSize,
          },
        },
        legend: {
          itemWidth: this.titleSize,
          itemHeight: this.titleSize,
          itemGap: this.titleSize / 2,
          textStyle: {
            fontSize: this.titleSize / 2,
          },
        },
        series: {
          radius: this.titleSize * 4.5,
          center: ["50%", "60%"],
        },
      };
      this.chartInstance.setOption(sizeOption);
      this.chartInstance.resize();
    },
    preview() {
      this.currentType--;
      if (this.currentType < 0) {
        this.currentType = this.data.length - 1;
      }
      this.updateEchart();
    },
    next() {
      this.currentType++;
      if (this.currentType > this.data.length - 1) {
        this.currentType = 0;
      }
      this.updateEchart();
    },
  },
};
</script>
<style scoped>
.point {
  color: #ffffff;
  position: absolute;
  top: 60%;
  left: 50%;
  transform: translate(-50%, -50%);
  /* font-size: 66px; */
  width: 90%;
}
.left {
  cursor: pointer;
}
.right {
  position: absolute;
  right: 0;
  cursor: pointer;
}
.title {
  position: absolute;
  bottom: 5%;
  right: 5%;
  font-size: 66px;
  color: #ffffff;
}
</style>